<?php
/*
 * 登录控制器
 */
namespace app\admin\controller;
use think\Request;
class Login extends Comm
{
    public function index()
    {
        $flag = $this->check_login();
        if ($flag) {
            $this->error('您已经登录,正在跳转到主页', url("index/index"));
        }
        return $this->fetch();
    }
    public function login()
    {
        $request = Request::instance();

        $verify = isset($_POST['verify']) ? trim($_POST['verify']) : '';
        if(!captcha_check($verify)){
            $this->error('验证码错误！', url("login/index"),null,1);
        };
        $username = isset($_POST['user']) ? trim($_POST['user']) : '';
        $password = isset($_POST['password']) ? password(trim($_POST['password'])) : '';
        $remember = isset($_POST['remember']) ? $_POST['remember'] : 0;
        if ($username == '') {
            $this->error('用户名不能为空！', url("login/index"));
        } elseif ($password == '') {
            $this->error('密码必须！', url("login/index"));
        }

        $model = db("Member");
        $user = $model->field('uid,user')->where(array('user' => $username, 'password' => $password))->find();

        if ($user) {
            $salt = config("my.COOKIE_SALT");
            $ip = $request->ip();
            $ua = $_SERVER['HTTP_USER_AGENT'];
            session('uid',$user['uid']);
            //加密cookie信息
//          $auth = password($user['uid'].$user['user'].$ip.$ua.$salt); //客户IP有CDN IP时时在变,不能加入IP
            $auth = password($user['uid'].$user['user'].'s1h0u'.$ua.$salt);
            if ($remember) {
                cookie('auth', $auth, 3600 * 24 * 7);//记住我
            } else {
                cookie('auth', $auth, 3600); //设置登录60分钟过期
            }
            addlog('登录成功。');
			$this->success('登录成功！',url('index/index'),null,1);
            //$url = url('index/index');
            //header("Location: $url");
            exit(0);
        } else {
            addlog('登录失败。', $username);
            $this->error('登录失败，请重试！', url("login/index"),null,1);
        }
    }
}